import Vue from 'vue'
import Vuex from 'vuex'
import http from '@/util/http'
import createPersistedState from 'vuex-persistedstate'

Vue.use(Vuex)

export default new Vuex.Store({
  plugins: [createPersistedState()],
  state: {
    cityId: '310100',
    cityName: '上海',
    cinemaList: []
  },
  mutations: {
    changeCityName (state, name) {
      state.cityName = name
    },
    changeCityId (state, id) {
      state.cityId = id
    },
    setCinemaList (state, cinemas) {
      state.cinemaList = cinemas
    },
    clearList (state) {
      state.cinemaList = []
    }
  },
  // 异步
  actions: {
    getCinemaList (store, cityId) {
      // TODO: 返回Promise对象，便于之后的初始化better-scroll操作
      return http({
        url: `/gateway?cityId=${cityId}&ticketFlag=1&k=8524925`,
        headers: {
          'X-Host': 'mall.film-ticket.cinema.list'
        }
      }).then(res => {
        store.commit('setCinemaList', res.data.data.cinemas)
      })
    }
  },
  modules: {
  }
})
